/*
 * Copyright (c) 2009, Justin Donaldson and the haXe Project Contributors 
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */

/*
 	Glue provides a simple method of attaching two functions together.  The 
	various function calls (joinVoid, join1, join2) refer to the arity of the
	functions that are being joined.  The result of a join function is a new
	function.  The function calls the first function argument (func1) and saves 
	the result.  Then it calls the second function (func2) as a side effect,
	discarding the result.  It returns the saved result from func1.
	All arguments are typed, so both functions (func1 and func2) must match
	argument arity.  However, return type can differ, since the result of func2
	is discarded.
*/

package org.sugar.sigslot;

class Glue{

	/*
 		Join two nullary matched functions together and return them as a new "glued"
		function.
	*/
	public static function joinVoid<A,B>(func1:Void->A, func2:Void->B) : Void->A {		
		return join(func1,func2);
	}
	/*
	 	Join two unary matched functions together and return them as a new "glued"
		function.
	*/
	public static function join1<A,B,C>(func1:A->B, func2:A->C) : A->B {

		return join(func1,func2);
	}
	/*
 		Join two binary matched functions together and return them as a new "glued"
		function.
	*/	
	public static function join2<A,B,C,D>(func1:A->B->C, func2:A->B->D) : A->B->C {
		return join(func1,func2);
	}
	/*
	 	Join two ternary matched functions together and return them as a new "glued"
		function.
	*/	
	public static function join3<A,B,C,D,E>(func1:A->B->C->D, func2:A->B->C->E) : A->B->C->D {
		return join(func1,func2);
	}
	/*
	 	Join two quaternary matched functions together and return them as a new "glued"
		function.
	*/
	public static function join4<A,B,C,D,E,F>(func1:A->B->C->D->E, func2:A->B->C->D->F) : A->B->C->D->E {
		return join(func1,func2);
	}
	/*
	 	Join two quinary matched functions together and return them as a new "glued"
		function.
	*/
	public static function join5<A,B,C,D,E,F,G>(func1:A->B->C->D->E->F, func2:A->B->C->D->E->G) : A->B->C->D->E->F {
		return join(func1,func2);
	}
	/*
	 	Join two senary matched functions together and return them as a new "glued"
		function.
	*/
	public static function join6<A,B,C,D,E,F,G,H>(func1:A->B->C->D->E->F->G, func2:A->B->C->D->E->F->H) : A->B->C->D->E->F->G {
		return join(func1,func2);
	}

	/*
	 	This is the private function call that actually performs the function rebinding.  It is not necessary
		to match function arity here, since the public functions already catch these.
	*/

	private static function join<A,B>(func1:A, func2:B) : A {
		
		var init = false;
		var f = function(args:Array<Dynamic>) {	
			var ret = Reflect.callMethod({}, func1, args);
			Reflect.callMethod({}, func2, args);
			return ret;
		}
		var fmv = Reflect.makeVarArgs(f);
		return  fmv;
	}
	
	/**
	 *  This is a memoize function that saves results under an argument hash, which can
	 *  speed up repeated calls with the same arguments.
	 *	@param func Dynamic A function
  	 *	@param hash_size Int The maximum size that the hash should grow to (in elements)
	 *	@return Dynamic The memoized function
	 */
	
	public static function memoize(func:Dynamic , hash_size:Int = 100) : Dynamic {

		var arg_hash = new Hash<Dynamic>();
		var f =  function(args:Array<Dynamic>){
			var arg_string = args.join('|');
			if (arg_hash.exists(arg_string)) return arg_hash.get(arg_string);
			else{
				var ret = Reflect.callMethod({},func,args);
				if(Lambda.count(arg_hash) < hash_size) arg_hash.set(arg_string, ret);
				return ret;
			}
		}
		f = Reflect.makeVarArgs(f);
		return f;
	}

	
	

}